package com.cloud.edu.sys.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.edu.contants.Constant;
import com.cloud.edu.sys.bean.MenuBean;
import com.cloud.edu.sys.dao.MenuBeanMapper;
import com.cloud.edu.sys.dao.MenuMapper;
import com.cloud.edu.sys.service.SysMenuService;
import com.cloud.edu.sys.service.SysRoleMenuService;
import com.cloud.edu.sys.service.SysUserService;

import java.util.ArrayList;
import java.util.List;


@Service("sysMenuService")
public class SysMenuServiceImpl  implements SysMenuService {
	@Autowired
	private MenuMapper menuMapper;
	
	@Autowired
	private MenuBeanMapper menuBeanMapper;
	
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	@Override
	public List<MenuBean> queryListParentId(String parentId, List<String> menuIdList) {
		List<MenuBean> menuList = queryListParentId(parentId);
		if(menuIdList == null){
			return menuList;
		}
		
		List<MenuBean> userMenuList = new ArrayList<>();
		for(MenuBean menu : menuList){
			if(menuIdList.contains(menu.getId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	@Override
	public List<MenuBean> queryListParentId(String parentId) {
		return menuBeanMapper.queryListParentId(parentId);
	}

	@Override
	public List<MenuBean> queryNotButtonList() {
		return null;// baseMapper.queryNotButtonList();
	}

	@Override
	public List<MenuBean> getUserMenuList(String userId) {
		//系统管理员，拥有最高权限
		if( Constant.SUPER_ADMIN.equals(userId)){
			return getAllMenuList(null);
		}
		
		//用户菜单列表
//		List<String> menuIdList = sysUserService.queryAllMenuId(userId);
//		return getAllMenuList(menuIdList);
		return null;
	}

	@Override
	public void delete(String menuId){
		//删除菜单
//		this.deleteById(menuId);
		//删除菜单与角色关联
//		sysRoleMenuService.deleteByMap(new MapUtils().put("menu_id", menuId));
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<MenuBean> getAllMenuList(List<String> menuIdList){
		//查询根菜单列表
		List<MenuBean> menuList = queryListParentId("0", menuIdList);
		//递归获取子菜单
		getMenuTreeList(menuList, menuIdList);
		
		return menuList;
	}

	/**
	 * 递归
	 */
	private List<MenuBean> getMenuTreeList(List<MenuBean> menuList, List<String> menuIdList){
		List<MenuBean> subMenuList = new ArrayList<MenuBean>();
		
		for(MenuBean entity : menuList){
			//目录
			if(entity.getType() == Constant.MenuType.CATALOG.getValue()){
				entity.setList(getMenuTreeList(queryListParentId(entity.getId(), menuIdList), menuIdList));
			}
			subMenuList.add(entity);
		}
		
		return subMenuList;
	}
}
